﻿// # define Debug 
using DL.Animation;
using DL.Characters.Player.FSM.Data;
using DL.Characters.Player.FSM.StateMachine;
using DL.Movement;
using UnityEngine;

namespace DL.Characters.Player.FSM.State
{
    public class PlayerMovementState : PlayerState
    {
        protected PlayerMoveStateMachine StateMachine { get; }
        protected Transform Self { get;}
        protected RuntimeReusableData ReusableData { get; }
        protected Animator Animator { get; }
        protected PlayerMoveController MoveController { get; }

        public PlayerMovementState(PlayerMoveStateMachine playerStateMachine, int property = 0) : base(property)
        {
            StateMachine = playerStateMachine;
            Self = StateMachine.Player.Self;
            ReusableData = StateMachine.Player.RuntimeReusableData;
            Animator = StateMachine.Player.Animator;
            MoveController = StateMachine.Player.MovementController;
        }

        public override void HandleInput()
        {
            ReusableData.InputDirection = GetPlayerMovementInputDirection();
            
            Animator.SetFloat(AnimatorParameter.Movement, MoveController.CurrentMovementVelocity.Value);
            Animator.SetFloat(AnimatorParameter.Horizontal, ReusableData.InputDirection.x, 0.2f, Time.deltaTime);
            Animator.SetFloat(AnimatorParameter.Vertical, ReusableData.InputDirection.y, 0.2f, Time.deltaTime);
        }

        public override void FixedUpdate()
        {
            //模拟重力,更新
            StateMachine.MovementController.SimulateChracterGravity();
            StateMachine.MovementController.CharacterVerticalVelocityInterface();
            
            if (ReusableData.CanRotation)
            {
                if (StateMachine.MovementController.CalculateTurnAngles(ReusableData.InputDirection))
                {
                    Animator.SetFloat(AnimatorParameter.DeltaAngle, ReusableData.TargetAngle);
                }
            }
            else
            {
                Animator.SetFloat(AnimatorParameter.DeltaAngle, 0f);
            }
        }
        
        protected Vector2 GetPlayerMovementInputDirection() => StateMachine.InputSystem.Movement;
        
        /// <summary>
        /// 计算旋转需要的值
        /// </summary>
        /// <returns>是否存在输入不为Default</returns>
        protected bool CalcCharacterRotationAngle()
        {
            //更新角色旋转 
            if (StateMachine.MovementController.CalculateTurnAngles(ReusableData.InputDirection))
            {
#if Debug
                Vector3 startPosition = PlayerTransform.position + PlayerTransform.up * 0.2f; // 角色当前位置
                Vector3 endPosition = startPosition +
                                      Quaternion.AngleAxis(ReusableData.DeltaAngle,
                                          PlayerTransform.up) * PlayerTransform.forward * 5; // 计算线段终点
                Debug.DrawLine(startPosition, endPosition, Color.red);
#endif
                return true;
            }
            return false;
        }
    }
}